﻿using System;

using TempEmployees.DAL;
using TempEmployees.BLL.EntitiesExtensions;

namespace TempEmployees.BLL.Validations
{
    public class SalaryField
    {
        private decimal m_salary;

        public decimal Salary { get { return m_salary; } }

        public SalaryField(decimal salary)
        {
            m_salary = salary;
            if (!IsValid(m_salary)) throw new ValidationErrorException();
        }

        /// <summary>
        /// Checks that the given salary for the assignment is valid.
        /// salary=0 means it equals to the base salary as stored in the DB. returns true. 
        /// else salary should be higher or equal to the base salary.
        /// </summary>
        /// <param name="salary">the given salary</param>
        /// <returns>true if valid, false else</returns>
        public static bool IsValid(decimal salary)
        {
            if (salary == 0) return true; //means the salary is the base salary as in the DB
            BaseSalary baseSalary;
            try
            {
                baseSalary = EBaseSalary.CurrentBaseSalary;
            }
            catch
            {
                return false;
            }
            if (salary < baseSalary.Salary) return false;
            return true;
        }

        public static bool IsValid(object salary)
        {
            try
            {
                return IsValid(decimal.Parse(salary.ToString()));
            }
            catch
            {
                return false;
            }
        }
    }
}